<template>
  <!-- 访客预约 -->
  <view class="container">
    <!-- #ifdef MP-WEIXIN -->
    <uni-nav-bar
      fixed
      :border="false"
      statusBar
      title=""
      color="#1F1F1F"
      left-icon="left"
      @clickLeft="onBack"
    >
    </uni-nav-bar>
    <!-- #endif -->
    <view class="alert">
      <text>访客预约</text>
    </view>


    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        :border="false"
        err-show-type="toast"
      >
        <uni-forms-item label="访客姓名" required name="visitorName">
          <uni-easyinput v-model="formData.visitorName" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="访客单位" required name="visitorUnit">
          <uni-easyinput v-model="formData.visitorUnit" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item
          label="访客身份证人像面"
          required
          name="idcardRenxiangOssId"
        >
          <FileUploader
            api="/workflow/visitorInvite/idcard"
            :limit="1"
            mode="grid"
            v-model="formData.idcardRenxiangOssId"
          />
        </uni-forms-item>
        <uni-forms-item label="访客身份证国徽面" name="idcardGuohuiOssId">
          <FileUploader
            api="/workflow/visitorInvite/idcard"
            :limit="1"
            mode="grid"
            v-model="formData.idcardGuohuiOssId"
          />
        </uni-forms-item>
        <uni-forms-item label="访客电话" required name="visitorPhone">
          <uni-easyinput v-model="formData.visitorPhone" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="来访前地址" required name="address">
          <uni-easyinput v-model="formData.address" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item
          label="访客身体状况"
          required
          name="visitorPhysicalCondition"
        >
          <uni-easyinput
            v-model="formData.visitorPhysicalCondition"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="来访方式" required name="visitWay">
          <uni-data-select
            placeholder="请选择"
            :localdata="visitWay"
            v-model="formData.visitWay"
          />
        </uni-forms-item>
        <uni-forms-item label="来访事由" required name="visitReason">
          <uni-easyinput
            type="textarea"
            v-model="formData.visitReason"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="来访日期" required name="date">
          <uni-datetime-picker
            type="datetimerange"
            hide-second
            v-model="formData.date"
            :clear-icon="false"
            :start="new Date().getTime()"
          />
        </uni-forms-item>
        <uni-forms-item
          label="来访时长"
          name="hours"
          v-if="formData.date.length !== 0"
        >
          <view
            >{{ `${formData.date[0]} ~ ${formData.date[1]}` }}<br />共计
            {{ formData.hours }} 小时</view
          >
        </uni-forms-item>
        <uni-forms-item label="选择受访人" required name="entourageUserId">
          <uni-data-select
            placeholder="请选择"
            :localdata="userData"
            v-model="formData.entourageUserId"
          />
        </uni-forms-item>
        <uni-forms-item label="备注" name="described">
          <uni-easyinput
            type="textarea"
            v-model="formData.described"
            placeholder="请输入"
          />
        </uni-forms-item>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { getUserListNoToken } from '@/api/index';
import { addH5VisitorInvite } from '@/api/workflow';
import FileUploader from '@/components/FileUploader/index.vue';
export default {
  components: {
    FileUploader,
  },
  data() {
    return {
      query: {},
      // 表单数据
      visitWay: [
        {
          text: '驾车',
          label: '驾车',
          value: '1',
        },
        {
          text: '出租车/网约车',
          label: '出租车/网约车',
          value: '2',
        },
        {
          text: '步行',
          label: '步行',
          value: '3',
        },
        {
          text: '骑行',
          label: '骑行',
          value: '4',
        },
      ],
      formData: {
        date: [],
        idcardGuohuiOssId: [],
        idcardRenxiangOssId: [],
      },
      userData: [],
      // 规则
      rules: {
        visitorName: {
          rules: [
            {
              required: true,
              errorMessage: '姓名不能为空',
            },
          ],
        },
        visitorUnit: {
          rules: [
            {
              required: true,
              errorMessage: '请输入访客单位内容',
            },
          ],
        },
        visitReason: {
          rules: [
            {
              required: true,
              errorMessage: '请输入来访事由内容',
            },
          ],
        },
        address: {
          rules: [
            {
              required: true,
              errorMessage: '请输入来访前地址内容',
            },
          ],
        },
        visitWay: {
          rules: [
            {
              required: true,
              errorMessage: '请选择来访方式内容',
            },
          ],
        },
        visitorPhysicalCondition: {
          rules: [
            {
              required: true,
              errorMessage: '请输入访客身体状况内容',
            },
          ],
        },
        date: {
          rules: [
            {
              required: true,
              errorMessage: '请选择日期',
            },
          ],
        },
        entourageUserId: {
          rules: [
            {
              required: true,
              errorMessage: '请选择受访人',
            },
          ],
        },
        idcardRenxiangOssId: {
          rules: [
            {
              required: true,
              errorMessage: '请上传身份证照片',
            },
          ],
        },
        visitorIdCard: {
          rules: [
            {
              required: true,
              errorMessage: '内容不能为空',
            },
            {
              pattern:
                /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))([0-2]\d|30|31)\d{3}[0-9Xx]$/,
              errorMessage: '请输入正确的身份证号码',
            },
          ],
        },
        visitorPhone: {
          rules: [
            {
              required: true,
              errorMessage: '访客电话不能为空',
            },
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码',
            },
          ],
        },
      },
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {
        console.log('newVal', newVal);
        if (newVal.length !== 0) {
          this.formData.hours = (
            (Date.parse(new Date(newVal[1])) -
              Date.parse(new Date(newVal[0]))) /
            (1000 * 60 * 60)
          ).toFixed(2);
        }
      },
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    this.formData.date = [
      this.$util.timestampToTime(
        new Date().getTime() + 1000 * 60 * 60 * 24,
        'YYYY-MM-DD HH:mm'
      ),
      this.$util.timestampToTime(
        new Date().getTime() + 1000 * 60 * 60 * 24 * 2,
        'YYYY-MM-DD HH:mm'
      ),
    ];
    this.query = query;
    getUserListNoToken().then((res) => {
      this.userData = res.data.map((item) => {
        return {
          label: item.userName,
          text: item.userName,
          value: item.userId,
        };
      });
    });
  },
  onShow() {},
  onUnload() {},
  methods: {
    // 提交
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          console.log('success', res);
          // loading提示
          uni.showLoading({ title: '操作中' });
          // 提交接口
          let api = addH5VisitorInvite;
          let params = {
            ...res,
            visitTime: res.date[0] + ':00',
            leaveTime: res.date[1] + ':00',
            applicationType: this.query.type,
            idcardRenxiangOssId: res.idcardRenxiangOssId
              .map((item) => item.ossId)
              .join(),
          };
          if (res.idcardGuohuiOssId) {
            params.idcardGuohuiOssId = params.idcardGuohuiOssId
              .map((item) => item.ossId)
              .join();
          }
          api(params).then((res) => {
            uni.hideLoading();
            uni.showToast({
              title: `提交成功`,
            });
            // 重置
            this.onReset();
          });
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 0rpx 36rpx;
  // #ifdef MP-WEIXIN
  padding-top: calc(constant(safe-area-inset-top));
  padding-top: calc(env(safe-area-inset-top));
  // #endif
  padding-bottom: calc(var(--window-bottom) + 100px);
  display: flex;
  flex-direction: column;

  .alert {
    height: 50px;
    margin: 20px 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    text {
      font-size: 24px;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 40%;
      color: #333333;
      font-size: 20px;
      font-weight: bold;
      position: relative;
      uni-text {
        white-space: nowrap;
      }

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 40rpx);
    .btn {
      width: 100%;
      height: 40px;
      font-size: 24px;
      color: #ffffff;
      line-height: 40px;
      background: #3681ff;
    }
  }
}
// $pad: '(min-width: 500px) and (max-width: 1366px)'; //平板
// $phone: '(min-width: 0px) and (max-width: 500px)'; //手机
@media (min-width: 500px) and (max-width: 1366px) {
  .container {
    min-height: calc(100vh - var(--window-bottom) - 200rpx);
    padding: 0rpx 36rpx;
    // #ifdef MP-WEIXIN
    padding-top: calc(constant(safe-area-inset-top));
    padding-top: calc(env(safe-area-inset-top));
    // #endif
    padding-bottom: calc(var(--window-bottom) + 150px);
    display: flex;
    flex-direction: column;

    .alert {
      height: 50px;
      margin: 20px 0;
      text-align: center;
      background: #ddeafa;
      border-radius: 12px;
      display: flex;
      justify-content: center;
      align-items: center;
      text {
        font-size: 24px;
        font-weight: 600;
        color: #3a84ff;
      }
    }

    .myForm {
      padding: 24rpx;
      background: #fff;
      border-radius: 12rpx;

      ::v-deep .uni-forms-item__label {
        width: 40%;
        color: #333333;
        font-size: 28px;
        font-weight: bold;
        position: relative;
        margin: 5px 0 10px;
        uni-text {
          white-space: nowrap;
        }

        &::after {
          // content: "";
          width: 100%;
          height: 1rpx;
          background: #000;
          position: absolute;
          left: 14rpx;
          bottom: 20rpx;
        }
      }

      ::v-deep .uni-forms-item__content {
        font-size: 20px;
        .uni-easyinput__content-input,
        .uni-date__x-input,
        .uni-select {
          height: 50px;
          line-height: 50px;
          font-size: 24px;
        }
        .uni-input-placeholder.uni-easyinput__placeholder-class,
        .uni-select__input-text.uni-select__input-placeholder,
        .uni-textarea-placeholder.uni-easyinput__placeholder-class {
          font-size: 20px;
        }
        .checklist-text,
        .uni-select__selector-item {
          font-size: 24px !important;
        }
        .uni-date-single--x {
          transform: scale(1.3);
          transform-origin: 0 0;
        }
        .uni-date-range--x {
          transform: scale(1.1);
          transform-origin: 0 0;
        }
        .file-title,
        .uni-easyinput__content-textarea {
          font-size: 20px;
        }
        .uni-icons {
          font-size: 30px;
        }
      }
      .myLabel {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .text {
          color: #333333;
          font-size: 28rpx;
          font-weight: bold;
        }
        .star {
          color: #dd524d;
        }
      }
    }

    .btnBox {
      position: fixed;
      left: 36rpx;
      right: 36rpx;
      bottom: calc(var(--window-bottom) + 50rpx);
      .btn {
        width: 100%;
        height: 60px;
        font-size: 24px;
        color: #ffffff;
        line-height: 60px;
        background: #3681ff;
      }
    }
  }
}
</style>
